﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Game
{
    class Arrow
    {
        private PictureBox pictureBox1;
        private Timer t;
        private SortedSet<Penguin> penguins;

        public Arrow(int y,Form1 form,SortedSet<Penguin> collection)
        {
            penguins = collection;
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();

            this.pictureBox1.BackgroundImage = global::Game.Resource1.arrow;
            this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.pictureBox1.Location = new System.Drawing.Point(40, y+40);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(103, 10);
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Show();
            form.Controls.Add(pictureBox1);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            BeginMove();
        }


        public int X
        {
            get
            {
                return pictureBox1.Location.X;
            }
        }

        public int Y
        {
            get
            {
                return pictureBox1.Location.Y;
            }
        }


        private void MoveLeft(int dx)
        {
            foreach (var peng in penguins)
            {
                if (this.Y >= peng.Y && this.Y <= peng.Y + peng.Height && peng.X >= this.X && peng.X <= this.X + dx)
                {
                    peng.Destroy();
                }
            }

            pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X+dx, pictureBox1.Location.Y);
            if (pictureBox1.Location.X > 2000)
            {
                t.Stop();
                t.Dispose();
                pictureBox1 = null;
            }
        }

        private void BeginMove()
        {
            t = new Timer();
            t.Interval = 20;
            t.Tick += new EventHandler(delegate { this.MoveLeft(50); });
            t.Start();
        }
    }
}
